package Game5;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

public class TankServer {
    private static int ID=100;
    public static final int TCP_PORT=8888;
    public static final int UDP_PORT = 6666;
    List<Client>clients=new ArrayList<>();
    public void start() {
        new Thread(new UDPThread()).start();
        ServerSocket ss=null;
        try {
            ss=new ServerSocket(TCP_PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            Socket s=null;
            try {
                s=ss.accept();
                DataInputStream dis=new DataInputStream(s.getInputStream());
                String IP=s.getInetAddress().getHostAddress();
                int udpPort=dis.readInt();
                Client c=new Client(IP,udpPort);
                clients.add(c);
                DataOutputStream dos=new DataOutputStream(s.getOutputStream());
                dos.writeInt(ID++);
                System.out.println("A Client Connect! Addr- " + s.getInetAddress() + ":" + s.getPort() + "----UDP Port:" + udpPort);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(s!=null) {
                        s.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        new TankServer().start();
    }
     private class Client {
        String IP;
        int udpPort;

         public Client(String IP, int udpPort) {
             this.IP = IP;
             this.udpPort = udpPort;
         }
     }
     //实现UDP线程，完成UDP信息的传输
     private class UDPThread implements Runnable {

         byte[]buffer=new byte[1024];
         @Override
         public void run() {
             DatagramSocket ds=null;
             try {
                 ds=new DatagramSocket(UDP_PORT);
             } catch (SocketException e) {
                 e.printStackTrace();
             }
             System.out.println("UDP thread started at port :" + UDP_PORT);
             while (ds!=null) {
                 DatagramPacket dp=new DatagramPacket(buffer,0,buffer.length);
                 try {
                     ds.receive(dp);
                     //在接收到Client端的包后，Server需要把信息传送给每个客户端
                     for(int i=0;i<clients.size();i++) {
                         Client c=clients.get(i);
                         dp.setSocketAddress(new InetSocketAddress(c.IP,c.udpPort));
                         ds.send(dp);
                     }
                     System.out.println("a packet received!");
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }
}
